package com.guigu.order.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.base.entity.GoodsType;
import com.guigu.base.entity.vo.OrderDashboardVO;
import com.guigu.order.entity.Order;
import com.guigu.order.entity.OrderCargo;
import com.guigu.order.entity.dto.OrderDTO;
import com.guigu.order.entity.query.OrderQuery;
import com.guigu.order.feign.BaseFeign;
import com.guigu.order.feign.UserAddressFeign;
import com.guigu.order.service.OrderService;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import com.guigu.user.entity.query.AddressBookQuery;
import com.guigu.user.entity.vo.AddressBookVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-09
 */
@RestController
@RequestMapping("/order/order")
@Api(tags = "订单管理")
public class OrderController {
    @Autowired
    private UserAddressFeign userAddressFeign;
    @Autowired
    private OrderService orderService;
    @Autowired
    private BaseFeign baseFeign;

    /**
     * 查询
     *
     * @param query
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation("查询地址簿信息")
    @PostMapping("/queryAddressBookBasedOnUserId/{pageSize}/{pageNum}")
    public R<Page<AddressBookVO>> queryAddressBookBasedOnUserId(@RequestBody AddressBookQuery query,
                                                                @PathVariable("pageSize") String pageSize,
                                                                @PathVariable("pageNum") String pageNum) {
        return userAddressFeign.queryAddressBookBasedOnUserId(query, pageSize, pageNum);
    }


    /**
     * 下单
     *
     * @param orderDTO
     * @return
     */
    @PostMapping("/")
    @ApiOperation("下订单")
    @ApiImplicitParam(name = "orderDTO", value = "订单传输对象")
    public R addOrder(@RequestBody OrderDTO orderDTO) {
        orderService.addOrder(orderDTO);
        return new R(ResultCode.SUCCESS_CODE, "下单成功!");
    }

    /**
     * 订单超时
     *
     * @param orderId
     * @return
     */
    @ApiOperation("订单超时")
    @ApiImplicitParam(name = "orderId", value = "订单Id")
    @PutMapping("/{orderId}")
    public R changeOrderTimeout(@PathVariable("orderId") String orderId) {
        R r = this.orderService.changeOrderTimeout(orderId);
        return r;
    }


    /**
     * 条件查询订单
     *
     * @param orderQuery
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderQuery", value = "查询对象"),
            @ApiImplicitParam(name = "pageNum", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页数")
    })
    @PostMapping("/getOrderInfo/{pageNum}/{pageSize}")
    public R<Page<Order>> getOrderInfo(@RequestBody OrderQuery orderQuery,
                                       @PathVariable Integer pageNum,
                                       @PathVariable Integer pageSize) {
        R<Page<Order>> r = this.orderService.getOrderInfo(orderQuery, pageNum, pageSize);
        return r;
    }

    /**
     * 获取所有货物类型
     *
     * @return
     */
    @ApiOperation("获取所有货物类型")
    @GetMapping("/getGoodsType")
    public R<List<GoodsType>> getGoodsType() {
        R<List<GoodsType>> r = baseFeign.getGoodsType();
        return r;
    }


    /**
     * 获取订单详情信息
     *
     * @param orderId
     * @return
     */
    @ApiOperation("根据id查询订单,订单重量,订单位置信息")
    @ApiImplicitParam(name = "orderId", value = "订单Id")
    @GetMapping("/getOrderInfoByOrderId/{orderId}")
    public R<Map> getOrderInfoByOrderId(@PathVariable String orderId) {
        R<Map> r = orderService.getOrderCargoVoAndOrderLocationVoInfoByOrderId(orderId);
        return r;
    }

    /**
     * 根据id动态修改status
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("根据id改状态")
    @ApiImplicitParam(name = "orderId", value = "订单Id")
    @GetMapping("/updateStatusById/{id}/{status}")
    public R updateStatusById(@PathVariable("id") String id,@PathVariable("status") Integer status) {
        Order order=new Order();
        order.setId(id);
        order.setStatus(status);
        orderService.updateById(order);
        return new R(ResultCode.SUCCESS_CODE,null);
    }


    /**
     * 查询订单状态是待取件并且是已付的状态
     *
     * @param status        订单状态
     * @param paymentStatus 付款状态
     * @return
     */
    @ApiOperation("查询订单状态是待取件并且是已付的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "订单状态"),
            @ApiImplicitParam(name = "paymentStatus", value = "付款状态")
    })
    @GetMapping("/getOrderList/{status}/{paymentStatus}")
    public List<Order> getOrderList(@PathVariable("status") Integer status,
                                    @PathVariable("paymentStatus") Integer paymentStatus) {
        List<Order> orders = this.orderService.getOrderList(status, paymentStatus);
        return orders;
    }

    /**
     * 修改订单状态为待发车装填
     *
     * @return
     */
    @ApiOperation("修改订单状态为待发车装填")
    @PutMapping("/modifyOrderStatus/{id}")
    public R modifyOrderStatus(@PathVariable("id") String id) {
        Order order = new Order();
        order.setId(id);
        order.setStatus(5);
        orderService.updateById(order);
        return new R(ResultCode.SUCCESS_CODE, "修改订单状态成功!", true);
    }

    /**
     * 根据订单Id获取订单信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据订单Id获取订单信息")
    @GetMapping("/getOrderById/{id}")
    public Order getOrderById(@PathVariable("id") String id) {
        Order order = orderService.getById(id);
        if (ObjectUtils.isEmpty(order)) {
            return null;
        }
        return order;
    }

    /**
     * 获取订单基础数据面板
     * @param dateCount 天数
     * @return
     */
    @ApiOperation("获取订单基础数据面板")
    @GetMapping("/getOrderDashboardData/{dateCount}")
    public R<OrderDashboardVO> getOrderDashboardData(@PathVariable Integer dateCount) {
        OrderDashboardVO vo = orderService.getOrderDashboardData(dateCount);
        return new R<>(ResultCode.SUCCESS_CODE, vo);
    }
    /**
     * 修改订单状态为待发车装填
     *
     * @return
     */
    @ApiOperation("根据用户ID查询订单列表")
    @GetMapping("/getOrderListByMemberId/{memberId}")
    public R getOrderListByMemberId(@PathVariable("memberId") String memBerId) {
        Map<String, Object> data = orderService.getOrderListByMemberId(memBerId);

        return new R(ResultCode.SUCCESS_CODE, "查询订单列表成功!", data);
    }

}

